package com.example.hello.controller;

import com.example.hello.pojo.LoginInfo;
import com.example.hello.pojo.Res;
import com.example.hello.pojo.User;
import com.example.hello.pojo.UserBorrowCertificate;
import com.example.hello.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PutMapping("/update")
    public Res update(
            @RequestParam("userId") Integer userId,
            @RequestBody LoginInfo loginInfo
            ){
        log.info("要将用户id：{}，修改为{}",userId,loginInfo);
        userService.updateById(userId,loginInfo);
        return Res.success();
    }

    /**
     * GET /user/{id}
     * 根据用户 ID 查询用户详情
     */
    @GetMapping("/{id}")
    public Res getUser(@PathVariable Long id) {
        User user = userService.getUserById(id);
        if (user == null) {
            return Res.error("用户未找到，ID=" + id);
        }
        return Res.success(user);
    }

    @GetMapping("/certificate/{id}")
    public Res getCertificate(@PathVariable Long id) {
        UserBorrowCertificate userBorrowCertificate = userService.getCertificateById(id);

        return Res.success(userBorrowCertificate);
    }



}
